package com.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.DefaultComboBoxModel;

import com.controlador.Conexion;
import com.controlador.Operaciones;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;

public class RegistroEditar extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JComboBox<?> cbProceso;
	private JCheckBox chckbxReuse;
	private JComboBox<?> cbIngeniero;
	private JComboBox<?> cbTipo;
	private JTextField txtSerie;
	private JTextField txtUsuario;
	private String ID;
	
	Conexion connex = new Conexion();
	Connection conn = null;
	Operaciones op = new Operaciones();
	private JLabel lblProceso;
	private JLabel lblNoSerie;
	private JLabel lblUsuario;
	private JLabel lblIngeniero;
	private JLabel lblTipo;
	private JComboBox<?> cbModelo;
	private JLabel lblModelo;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegistroEditar frame = new RegistroEditar();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public RegistroEditar() {
		setTitle("..::Edit::..");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 700, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblEdicin = new JLabel("Edit");
		
		lblProceso = new JLabel("Process:");
		
		cbProceso = new JComboBox();
		cbProceso.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				int opc = cbProceso.getSelectedIndex();
				
				switch (opc) {
				case  0:
					unsetAllVisible();
					setReturnOptions();
					
					break;
				case  1:
					unsetAllVisible();
					setReuseDeliveryOptions();
									
					break;
				case  2:
					unsetAllVisible();
					setLoanDeliveryOptions();
					
					break;
				case  3:
					unsetAllVisible();
					setNewDeliverOptions();
					
					break;
				case  4:
					unsetAllVisible();
					setShellSwapOptions();
					
					break;
				case  5:
					unsetAllVisible();
					setNewPCInOptions();
					
					break;

				}
			
			}
		});
		cbProceso.setModel(new DefaultComboBoxModel(new String[] {"Return", "Reuse Delivery", "Loan Delivery", "New Deliver", "Shell Swap", "New PC In"}));
		
		chckbxReuse = new JCheckBox("Reuse");
		chckbxReuse.setVisible(false);
		
		lblNoSerie = new JLabel("Serial No.");
		
		txtSerie = new JTextField();
		txtSerie.setColumns(10);
		
		lblUsuario = new JLabel("User");
		
		txtUsuario = new JTextField();
		txtUsuario.setColumns(10);
		
		lblIngeniero = new JLabel("Engineer");
		
		cbIngeniero = new JComboBox();
		cbIngeniero.setModel(new DefaultComboBoxModel(new String[] {"Juan", "Kike", "Omar", "Tino"}));
		
		lblTipo = new JLabel("Type");
		
		cbTipo = new JComboBox();
		cbTipo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(cbTipo.getSelectedIndex()==1){
					cbModelo.setVisible(false);
					lblModelo.setVisible(false);
					cbModelo.setSelectedIndex(0);
				}
				else
				{
					cbModelo.setVisible(true);
					lblModelo.setVisible(true);
				}
			}
		});
		cbTipo.setModel(new DefaultComboBoxModel(new String[] {"Laptop", "Workstation"}));
		
		JButton btnGuardar = new JButton("Save");
		btnGuardar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
			/*UPDATE*/
				conn=connex.AccederBD();
				String cyv = new String();
				
				if (chckbxReuse.isVisible())
				{										
					//en este bloque checamos si esta activo el checkbox de return
					String siono= new String();
					if(chckbxReuse.isSelected())
					{
						siono="si";
					}
					else{
						siono="no";
					}
					
					cyv = "noSerie=\'"+txtSerie.getText()+"\', tipo=\'"+cbTipo.getSelectedItem().toString()+
							"\', proceso=\'"+cbProceso.getSelectedItem().toString()+"\', reuso=\'"+siono+"\', idIngeniero=\'"+cbIngeniero.getSelectedItem().toString()+
							"\', usuario=\'"+txtUsuario.getText()+"\', modelo=\'"+cbModelo.getSelectedItem().toString()+"\'";
				}else
				{
				
				cyv = "noSerie=\'"+txtSerie.getText()+"\', tipo=\'"+cbTipo.getSelectedItem().toString()+
						"\', proceso=\'"+cbProceso.getSelectedItem().toString()+"\', idIngeniero=\'"+cbIngeniero.getSelectedItem().toString()+
						"\', usuario=\'"+txtUsuario.getText()+"\', modelo=\'"+cbModelo.getSelectedItem().toString()+"\'";
				}
				
				
				cyv = cyv.replace("\'null\'", "\'\'");
				if(op.update("Equipos", cyv, "id="+ID, conn))
				{
					JOptionPane.showMessageDialog(null, "OK");
				}else
				{
					System.err.println("Error: no se pudo actualizar la informacion");
				}
				
				
				dispose();
			
			}
		});
		
		lblModelo = new JLabel("Model");
		
		cbModelo = new JComboBox();
		cbModelo.setModel(new DefaultComboBoxModel(new String[] {"", "6510b", "6455b", "6460b", "6470b", "6530b", "6730b", "6930b", "8440p", "8460p", "8470p", "9470m", "840 G1", "8550w", "8560w", "8570w", "Zbook", "810 Revolv", "2170p", "2540p", "2560p"}));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblEdicin)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblProceso)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(cbProceso, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(chckbxReuse))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblNoSerie)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtSerie, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lblUsuario)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtUsuario, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(lblIngeniero)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(cbIngeniero, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblTipo)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(cbTipo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lblModelo)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(cbModelo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(btnGuardar))
					.addContainerGap(190, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblEdicin)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblProceso)
						.addComponent(cbProceso, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(chckbxReuse))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNoSerie)
						.addComponent(txtSerie, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblUsuario)
						.addComponent(txtUsuario, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblIngeniero)
						.addComponent(cbIngeniero, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTipo)
						.addComponent(cbTipo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblModelo)
						.addComponent(cbModelo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(61)
					.addComponent(btnGuardar)
					.addContainerGap(50, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
		
	}
	
	
	public void populateInformacion(String datos)
	{
		
		String data[] = datos.split("\\|");
		int tamanio =data.length;
		
		ID= data[0];

		txtSerie.setText(data[1]);
	
		cbTipo.setSelectedIndex(getValorIndice(data[2], "tipo"));

		cbProceso.setSelectedIndex(getValorIndice(data[3], "proceso"));
		if(data[4].contentEquals("si"))
		{
			chckbxReuse.setVisible(true);
			chckbxReuse.setSelected(true);
			
		}
		cbIngeniero.setSelectedIndex(getValorIndice(data[6], "idIng"));
		//Si el campo usuario esta vacio, se omite el paso
		if(tamanio>8){
			txtUsuario.setText(data[7]);
			cbModelo.setSelectedItem(data[8]);
		}
		
		super.setVisible(true);		
		this.repaint();
		System.out.println(datos);
	}

	public int getValorIndice(String valor,String opcion)
	{
		int valorAIndice=0;
		
		switch (opcion) {
		
		case "proceso"://GRANDE
			switch (valor) {
			 case "Return":
				 valorAIndice=0;
				break;
			 case "Reuse Delivery":
				 valorAIndice=1;
				break;
			 case "Loan Delivery":
				 valorAIndice=2;
				break;
			 case "New Deliver":
				 valorAIndice=3;
				break;
			 case "Shell Swap":
				 valorAIndice=4;
				break;
			 case "New PC In":
				 valorAIndice=5;
				break;
			}
			
			break;
		case "tipo"://GRANDE
			 switch (valor) {
			  case "Laptop":
				  valorAIndice=0;
				break;
			  case "Workstation":
				  valorAIndice=1;
				break;
			}
			
			break;
		case "idIng"://GRANDE
			switch (valor) {
			case "Juan":
				valorAIndice=0;
				break;
			case "Kike":
				valorAIndice=1;
				break;
			case "Omar":
				valorAIndice=2;
				break;
			case "Tino":
				valorAIndice=3;
				break;
		}
		
		}
		
		return valorAIndice;
	}
	
	public void setReturnOptions()
	{
		lblProceso.setVisible(true);
		cbProceso.setVisible(true);
		chckbxReuse.setVisible(true);
		lblNoSerie.setVisible(true);
		txtSerie.setVisible(true);
		lblIngeniero.setVisible(true);
		cbIngeniero.setVisible(true);
		lblTipo.setVisible(true);
		cbTipo.setVisible(true);
		lblUsuario.setVisible(true);
		txtUsuario.setVisible(true);
		
	}
	
	public void setReuseDeliveryOptions()
	{
		lblProceso.setVisible(true);
		cbProceso.setVisible(true);
		lblNoSerie.setVisible(true);
		txtSerie.setVisible(true);
		lblUsuario.setVisible(true);
		txtUsuario.setVisible(true);
		lblIngeniero.setVisible(true);
		cbIngeniero.setVisible(true);
		lblTipo.setVisible(true);
		cbTipo.setVisible(true);
	}
	
	public void setLoanDeliveryOptions()
	{
		lblProceso.setVisible(true);
		cbProceso.setVisible(true);
	}
	
	public void setNewDeliverOptions()
	{
		lblProceso.setVisible(true);
		cbProceso.setVisible(true);
		lblNoSerie.setVisible(true);
		txtSerie.setVisible(true);
		lblUsuario.setVisible(true);
		txtUsuario.setVisible(true);
		lblIngeniero.setVisible(true);
		cbIngeniero.setVisible(true);
		lblTipo.setVisible(true);
		cbTipo.setVisible(true);
		
	}
	
	public void setShellSwapOptions()
	{
		lblProceso.setVisible(true);
		cbProceso.setVisible(true);
		lblNoSerie.setVisible(true);
		txtSerie.setVisible(true);
		lblUsuario.setVisible(true);
		txtUsuario.setVisible(true);
		lblIngeniero.setVisible(true);
		cbIngeniero.setVisible(true);
		lblTipo.setVisible(true);
		cbTipo.setVisible(true);
		
	}
	
	public void setNewPCInOptions()
	{
		/*lblProceso.setVisible(true);
		cbProceso.setVisible(true);
		lblNoSerie.setVisible(true);
		txtSerie.setVisible(true);
		lblTipo.setVisible(true);
		cbTipo.setVisible(true);*/
		lblProceso.setVisible(true);
		cbProceso.setVisible(true);
		lblNoSerie.setVisible(true);
		txtSerie.setVisible(true);
		lblUsuario.setVisible(true);
		txtUsuario.setVisible(true);
		lblIngeniero.setVisible(true);
		cbIngeniero.setVisible(true);
		lblTipo.setVisible(true);
		cbTipo.setVisible(true);
		
	}
	
	public void unsetAllVisible()
	{
		
		cbProceso.setVisible(false);
		chckbxReuse.setVisible(false);
		cbIngeniero.setVisible(false);
		cbTipo.setVisible(false);
		txtSerie.setVisible(false);
		txtUsuario.setVisible(false);
		lblProceso.setVisible(false);
		lblNoSerie.setVisible(false);
		lblUsuario.setVisible(false);
		lblIngeniero.setVisible(false);
		lblTipo.setVisible(false);
		
	}
	
	public void clearAllOptions() {
		
		txtSerie.setText("");
		txtUsuario.setText("");
		cbIngeniero.setSelectedIndex(0);
		//cbLoan.setSelectedIndex(0);
		chckbxReuse.setSelected(false);
		JOptionPane.showMessageDialog(null, "OK!");
	}
}
